package com.itheima.proxy;

import com.itheima.service.UserService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserServiceJDKProxy {

    public static UserService createUserServiceJDKProxy(final  UserService userService){


        ClassLoader clssLoader = userService.getClass().getClassLoader();

        Class<?>[] classes = userService.getClass().getInterfaces();

        InvocationHandler ih = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object obj = method.invoke(userService, args);
                //后置增强内容
                System.out.println("刮大白");
                return obj;
            }
        };
        UserService proxy = (UserService) Proxy.newProxyInstance(clssLoader,classes,ih);
        return proxy;
    }
}
